﻿using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;

namespace nhakhoachauau
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        public int SLBenhNhan;
        protected void Page_Load(object sender, EventArgs e)
        {
            SLBenhNhan = 0;
            if (!IsPostBack)
            {
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                lblTitle.Text = "Ngày " + dp1.Value;
            }
        }

        protected void ddlChonNam_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = DateTime.Now.Year; i >= 2000; i--)
                {
                    ddlChonNam.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
            }
        }

        protected void ddlThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNgay)
            {
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");

                rptThongKeTheoNgay.Visible = true;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Ngày " + dp1.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = true;
                div_ChonNam.Visible = false;
                txtChonThang.Value = DateTime.Now.Date.ToString("MM-yyyy");

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = true;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNam)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = true;

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = true;

                //lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
            btnThongKe_Click(sender, e);
        }

        protected void rptThongKeTheoNgay_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Q_BUSBenhNhan BUSbn = new Q_BUSBenhNhan();
                string[] arrValue = dp1.Value.Split('-');
                List<KhamBenh> dsBenhNhan = BUSbn.dsBenhNhanTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                if (dsBenhNhan == null)
                    dsBenhNhan = new List<KhamBenh>();
                SLBenhNhan = dsBenhNhan.Count;
                rptThongKeTheoNgay.DataSource = dsBenhNhan;
                rptThongKeTheoNgay.DataBind();
            }
        }

        protected void rptThongKeTheoNgay_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Q_BUSBenhNhan BUSbn = new Q_BUSBenhNhan();
                Q_BUSKhamBenh_DieuTri BUSkbdt = new Q_BUSKhamBenh_DieuTri();
                List<LoaiDieuTri> listDieuTri = new List<LoaiDieuTri>();
                KhamBenh kb = (KhamBenh)e.Item.DataItem;
                BenhNhan bn = null;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                Label lblBenhNhan = (Label)e.Item.FindControl("lblBenhNhan");
                Label lblSoHoSo = (Label)e.Item.FindControl("lblSoHoSo");
                listDieuTri = BUSkbdt.LayTenLoaiDieuTri(kb.id);
                ListView lvdieutri = (ListView)e.Item.FindControl("lvDieuTri");
                lvdieutri.DataSource = listDieuTri;
                lvdieutri.DataBind();
                bn = BUSbn.ThongTinBenhNhan(kb.id);
                lblBenhNhan.Text = bn.ho_ten;
                lblSoHoSo.Text = bn.so_ho_so.ToString();
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }

        protected void rptThongKeTheoThang_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                TKBenhNhanThang tkbnt = (TKBenhNhanThang)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                SLBenhNhan += tkbnt.TongSo;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        protected void rptThongKeTheoNam_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                TKBenhNhanNam tkbnnam = (TKBenhNhanNam)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                SLBenhNhan += tkbnnam.TongSo;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNgay)
            {
                Q_BUSBenhNhan BUSbn = new Q_BUSBenhNhan();
                string[] arrValue = dp1.Value.Split('-');
                List<KhamBenh> dsBenhNhan = BUSbn.dsBenhNhanTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                if (dsBenhNhan == null)
                    dsBenhNhan = new List<KhamBenh>();
                SLBenhNhan = dsBenhNhan.Count;
                rptThongKeTheoNgay.DataSource = dsBenhNhan;
                rptThongKeTheoNgay.DataBind();

                lblTitle.Text = "Ngày " + dp1.Value;

            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                Q_BUSKhamBenh BUSkb = new Q_BUSKhamBenh();
                string[] arrValue = txtChonThang.Value.Split('-');
                List<TKBenhNhanThang> dsBenhNhaThang = BUSkb.dsBenhNhanTheoThang(int.Parse(arrValue[0]), int.Parse(arrValue[1]));
                if (dsBenhNhaThang == null)
                    dsBenhNhaThang = new List<TKBenhNhanThang>();
                rptThongKeTheoThang.DataSource = dsBenhNhaThang;
                rptThongKeTheoThang.DataBind();
                lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else
            {
                Q_BUSKhamBenh BUSkb = new Q_BUSKhamBenh();
                int nam = int.Parse(ddlChonNam.Text);
                rptThongKeTheoNam.DataSource = BUSkb.dsBenhNhanTheoNam(nam);
                rptThongKeTheoNam.DataBind();
                lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
        }
    }
}